package com.example.dao.ClassDao;

import com.example.bean.Class;
import com.example.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ClassAddDaoImp implements ClassAddDao {

    @Override
    public int getClassAdd(Class cl) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            //1.建立连接
            connection = DBUtil.getConnection();
            //2.编写SQL语句
            String sql="insert into c_user values(null,?,?,?,?,?)";
            //3.预处理对象
            preparedStatement = connection.prepareStatement(sql);
            //4.设置占位符
            preparedStatement.setString(1,cl.getCname());
            preparedStatement.setString(2,cl.getCgrade());
            preparedStatement.setString(3,cl.getCtname());
            preparedStatement.setString(4,cl.getCflag());
            preparedStatement.setString(5,cl.getCnum());
            //5.受影响行数
            int i = preparedStatement.executeUpdate();
            return i;//表示添加成功
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6.释放有关的资源
            try {
                DBUtil.closeResource(connection,preparedStatement);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;//表示失败
    }
}
